﻿using ProjectResistance.Shell.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;

namespace ProjectResistance.Unit.Test
{
    [TestClass()]
    public class ResistorTest
    {
        public TestContext TestContext { get; set; }

        [TestMethod()]
        [Description("Ensures that a Resistor can be created")]
        public void ResistorCtorPositiveTest()
        {
            Resistor r = new Resistor();
            Assert.IsNotNull(r, "ctor returned null");
        }

        [TestMethod()]
        [Description("Ensures that setting a negative resistance throws")]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void NegativeResistanceTest()
        {
            Resistor r = new Resistor();
            r.Resistance = -1;
        }

        [TestMethod()]
        [Description("Ensures that we can read/write valid resistance")]
        public void ResistancePositiveTest()
        {
            decimal expected = 100;
            Resistor r = new Resistor();
            r.Resistance = expected;
            Assert.AreEqual<decimal>(expected, r.Resistance, "first read failed");
            expected = 770;
            r.Resistance = expected;
            Assert.AreEqual<decimal>(expected, r.Resistance, "second read failed");
        }

        [TestMethod()]
        [Description("Ensures that we can read/write valid tolerance")]
        public void TolerancePositiveTest()
        {
            ToleranceValues expected = ToleranceValues.PointOnePercent;
            Resistor r = new Resistor();
            r.Tolerance = expected;
            Assert.AreEqual<ToleranceValues>(expected, r.Tolerance, "first read failed");
            expected = ToleranceValues.TwentyPercent;
            r.Tolerance = expected;
            Assert.AreEqual<ToleranceValues>(expected, r.Tolerance, "second read failed");
        }

        [TestMethod()]
        [Description("Ensures that when we change resistance, the ResistanceChanged event fires")]
        public void ResistanceChangedEventTest()
        {
            AutoResetEvent are = new AutoResetEvent(false);
            Resistor r = new Resistor();
            r.ResistanceChanged += delegate
            {
                are.Set();
            };

            r.Resistance = 100;
            Assert.IsTrue(are.WaitOne(1000, false), "Event did not fire");
        }

        [TestMethod()]
        [Description("Ensures that when we change tolerance, the ToleranceChanged event fires")]
        public void ToleranceChangedEventTest()
        {
            AutoResetEvent are = new AutoResetEvent(false);
            Resistor r = new Resistor();
            r.ToleranceChanged += delegate
            {
                are.Set();
            };

            r.Tolerance = ToleranceValues.PointTwoFivePercent;
            Assert.IsTrue(are.WaitOne(1000, false), "Event did not fire");
        }
    }
}
